package com.autoMobile.servlet.rrm;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.autoMobile.bean.rrmBx;
import com.autoMobile.dao.rrmbxDao;

@WebServlet("/UpdateBxServlet")
public class UpdateBxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public UpdateBxServlet() {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idParam = request.getParameter("id");
        int id = -1;
        
        // 参数验证
        try {
            id = Integer.parseInt(idParam);
        } catch (NumberFormatException e) {
            response.getWriter().write("Invalid ID format.");
            return;
        }
        
        // 获取用户信息
        rrmbxDao dao = new rrmbxDao();
        rrmBx bx = dao.findUserById(id);
        
        if (bx != null) {
            request.setAttribute("bx", bx);
            request.getRequestDispatcher("BxServlet").forward(request, response);
        } else {
            response.getWriter().write("User not found.");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        
        // 获取表单参数
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println("upServletid号："+id);
        String bdh = request.getParameter("bdh");
        String bxzl = request.getParameter("bxzl");
        String bxgs = request.getParameter("bxgs");
        String csrq = request.getParameter("csrq");
        String jzrq = request.getParameter("jzrq");
        String cph = request.getParameter("cph");
        String cx = request.getParameter("cx");
        String ys = request.getParameter("ys");
        String cjh = request.getParameter("cjh");
        Double be = null;

        // 参数验证
        try {
            be = Double.parseDouble(request.getParameter("be"));
        } catch (NumberFormatException e) {
            response.getWriter().write("Invalid amount format.");
            return;
        }
        
        // 创建 Bx 对象并设置属性
        rrmBx bx = new rrmBx();
        bx.setId(id);
        bx.setBdh(bdh);
        bx.setBxzl(bxzl);
        bx.setBe(be);
        bx.setBxgs(bxgs);
        bx.setCsrq(csrq);
        bx.setJzrq(jzrq);
        bx.setCph(cph);
        bx.setCx(cx);
        bx.setYs(ys);
        bx.setCjh(cjh);
        
        // 输出调试信息
        System.out.println("更新测试：" + bx.toString());
        
        // 更新数据
        rrmbxDao dao = new rrmbxDao();
        int success = dao.UpdateBx(bx);
        
        if (success != 0) {
            response.sendRedirect("BxServlet");  // 更新成功，重定向到列表页面
        } else {
            response.getWriter().write("Failed to update user.");
        }
    }

    }
